//
//  InternationalControl.swift
//  DemoNavBar
//
//  Created by YangHuan on 2019/12/13.
//  Copyright © 2019 YangHuan. All rights reserved.
//

import UIKit


/// Override
///
/// - Parameter key: key
/// - Returns: Localized string
public func Localized(key: String, tableName: String? = "Localizable") -> String {
    return InternationalControl.string(str: key, tableName: tableName)
}

class InternationalControl: NSObject {

        static var bundle: Bundle? = nil
        
        class func bundles() -> Bundle {
            return (bundle ?? nil)!
        }
        
        class func initUserLanguage() -> Void {
            
            UITextField.appearance().tintColor = UIColor.lightGray
            
            var string: String = ""
            let def = UserDefaults.standard
            
            let defaultL: Any = (def.object(forKey: "default_language")) ?? ""
            let defaultLanguage = defaultL as! String
            
            
            let shared = UserDefaults(suiteName: "group.com.seblong.app.IDream")
            
            if defaultLanguage.count > 0 {
                var enStr: String = ""
                enStr = defaultLanguage.subString(to: 1)
                
                if enStr == "zh" {
                    string = "zh-Hans-CN"
                }else {
                    string = "en"
                }
            }else {
                let defs = UserDefaults.standard
                let languages: [String] = defs.object(forKey: "AppleLanguages") as! Array
                let current = languages[0]
                
                var enStr: String = ""
                if current.count > 1 {
                    enStr = current.subString(to: 1)
                }
                if enStr == "zh" {
                    string = "zh-Hans-CN"
                }else {
                    string = "en"
                }
            }
            def.setValue(string, forKey: "userLanguage")
            def.synchronize()
            
            shared?.setValue(string, forKey: "userLanguage")
            shared?.synchronize()
            
            var path = Bundle.main.path(forResource:string , ofType: "lproj")

            if path == nil {
                path = Bundle.main.path(forResource:string , ofType: "lproj")
            }
            bundle = Bundle(path: (path ?? nil)!)
            setUserlanguage(language: userLanguage())
        }
        
        class func userLanguage()->String {
            let def = UserDefaults.standard
            let language = def.value(forKey: "userLanguage") as! String
            return language
        }
        
        class func getUserLanguage()->String {
            
            let string = InternationalControl.userLanguage()
            
            let enStr = string.subString(to: 1)
            
            if enStr == "zh" {
                return "简体中文"
            }else {
                return "English"
            }
        }
        
        class func setUserlanguage(language: String)->Void {
            let def = UserDefaults.standard
            
            let path = Bundle.main.path(forResource:language , ofType: "lproj")
            bundle = Bundle(path: path!)
            
            def.setValue(language, forKey: "userLanguage")
            def.synchronize()
            
            let shared = UserDefaults(suiteName: "group.com.seblong.app.IDream")
            shared?.setValue(language, forKey: "userLanguage")
            shared?.synchronize()
        }
        
        class func string(str: String, tableName: String? = "Localizable")->String {
            let myStr = self.bundle?.localizedString(forKey: str, value: nil, table: tableName)
    //        Localizable
            
            return myStr ?? ""
        }

        
    }

